package com.qk.management.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.management.mapper.DeptMapper;
import com.qk.management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
@SuppressWarnings("all")
public class DeptServiceImpl implements DeptService {


    @Autowired
    private DeptMapper deptMapper;

    @Override
    public PageResult<Dept> page(String name, Integer status, Integer page, Integer pageSize) {

        // 1 查询满足条件总记录数
        Integer total = deptMapper.count(name, status);
        // 2. 查询当前页数据列表
        // 计算偏移量 优化
        Integer offset = (page-1)*pageSize;
        List<Dept> rows = deptMapper.selectByPage(name, status, offset, pageSize);
        // TODO 缺陷修复 N 种方法
        // 3. 数据组装
        return PageResult.<Dept>builder().total(total).rows(rows).build();
    }

    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public void deleteDept(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public void updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateById(dept);
    }

    @Override
    public Dept getDeptById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public List<Dept> list() {
        return deptMapper.selectAll();
    }

}
